package com.xiaoyao.novel.action.page;

import java.util.List;

import com.xiaoyao.novel.common.Pagin;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.service.BookService;
import com.xiaoyao.novel.service.ChapterService;
import com.xiaoyao.novel.velocity.PageVelocityStatic;
import com.xiaoyao.novel.velocity.PhoneVelocityStatic;

public class ChapterAction extends NovelAction {
	private static final long serialVersionUID = 1L;
	ChapterService chapterService = new ChapterService();
	BookService bookService = new BookService();
	Chapter chapter;
	Pagin pagin = new Pagin();

	// //////////////////////////////////////

	public String chapter() throws Exception {
		getConfig();
		listCategory();
		getFooter();
		getHot(20);
		chapter = chapterService.getById(chapter.cid);
		if (chapter!=null) { 
//			// 两个可以合并成一条ＳＱＬ
//			chapter.setNextId(chapterService.getNextChapter(chapter.bookId,chapter.cid)) ;
//			chapter.setPrevId(chapterService.getPrevChapter(chapter.bookId,chapter.cid)) ;
			List<Integer> idList=chapterService.getPrevNextChapter(chapter.bookId,chapter.cid);
			chapter.setPrevId (idList.get(0));
			chapter.setNextId(idList.get(1));
			Book book = bookService.getById(chapter.bookId);
			request.setAttribute("chapter", chapter);
			request.setAttribute("book", book);
		} 		
		return "chapter";
	}

	// //////////////////////////////////////
	@Override
	public String execute() throws Exception {
		return search();
	}

	public String search() throws Exception { 
		pagin.getSearchParam().put("orderBy",  " cid desc ");
		chapterService.search(pagin);
		request.setAttribute("pagin", pagin);
		return SUCCESS;
	}

	public String deleteChapter() throws Exception {
		chapterService.delete(chapter.getCid());// 删除相关数据.更新首页
		return execute();
	}

	public String toAddChapter() throws Exception {
		request.setAttribute("chapter", chapter);
		return "showAdd";
	}

	public String addChapter() throws Exception {

		chapterService.insert(chapter);
		request.setAttribute("msg", "添加成功");
		//书籍章节记录
		Book book = bookService.getById(chapter.bookId);
		book.setSavedCount(book.getSavedCount()+1);
		book.setNewChapter(chapter.getChapterTitle());
		bookService.update(book);
		
		PageVelocityStatic.staticBookAndChapters(book, 2);
		PhoneVelocityStatic.staticBookAndChapters(book, 2);
 
		pagin.getSearchParam().put("bookId", chapter.getBookId()+"");
		return execute();
	}

	public String showModify() throws Exception {
		chapter = chapterService.getById(chapter.getCid());
		request.setAttribute("chapter", chapter);
		return "showModify";
	}

	public String modifyChapter() throws Exception {
		chapterService.update(chapter);
		return execute();
	}

	// ////////////////////

	public ChapterService getChapterService() {
		return chapterService;
	}

	public void setChapterService(ChapterService chapterService) {
		this.chapterService = chapterService;
	}

	public Chapter getChapter() {
		return chapter;
	}

	public void setChapter(Chapter chapter) {
		this.chapter = chapter;
	}

	public Pagin getPagin() {
		return pagin;
	}

	public void setPagin(Pagin pagin) {
		this.pagin = pagin;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
